﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace atmsystem_ss
{
    public partial class frmInterfaceWelcome : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dtAdmin = new DataTable("tblAdmin");
        private DataTable dtAccount = new DataTable("tblAccount");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        public frmInterfaceWelcome()
        {
            InitializeComponent();
        }

        private void tabAdmin_Click(object sender, EventArgs e)
        {

        }
        private bool IsNumber(String str)
        {
            foreach (Char c in str)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
        private bool tontai()
        {
            dtAccount.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblAccount where fldID_Account LIKE '%'+@fldID_Account+'%' ";
            command.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
            da.SelectCommand = command;
            da.Fill(dtAccount);
            if (dtAccount.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        private bool tontai_PIN_Account()
        {
            dtAccount.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblAccount where (fldID_Account LIKE '%'+@fldID_Account+'%') and (fldPIN LIKE '%'+@fldPIN+'%') ";

            command.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
            command.Parameters.Add("@fldPIN", SqlDbType.Char, 4).Value = txtPIN.Text;

            da.SelectCommand = command;
            da.Fill(dtAccount);
            if (dtAccount.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        private bool kiemtraPIN()
        {
            kt = true;
            if (txtPIN.Text == "")
            {
                if (MessageBox.Show("Please enter your PIN, PIN is the string of 4 number, Click OK to continue, click Cancel to exit", "Error", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    txtPIN.Focus();
                    kt = false;
                }
                else
                {
                    labID_Account.Visible = true;
                    txtID_Account.Visible = true;
                    btnNext.Visible = true;
                    labPIN.Visible = false;
                    txtPIN.Visible = false;
                    btnLogin_Account.Visible = false;
                    btnCancel.Visible = false;
                }
            }
            else if (IsNumber(Convert.ToString(txtPIN.Text)) == false)
            {
                txtPIN.Text = "";
                txtPIN.Focus();
                MessageBox.Show("Your PIN is the string of 4 number, Please check back your PIN", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                kt = false;
            }
            else if (Convert.ToString(txtPIN.Text).Length.Equals(4) == false)
            {
                txtPIN.Text = "";
                txtPIN.Focus();
                MessageBox.Show("Your PIN is the string of 4 number, Please check back your PIN", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                kt = false;
            }
            else if (tontai_PIN_Account() == true)
            {
                txtPIN.Text = "";
                MessageBox.Show("PIN null, Please check back your PIN ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                kt = false;
            }
            return kt;
        }
        private bool kiemtraAccount()
        {
        kt = true;
        if (txtID_Account.Text == "")
        {
            txtID_Account.Focus();
            MessageBox.Show("Please enter Account ID before click button Next", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            kt = false;
        }
        else if (IsNumber(Convert.ToString(txtID_Account.Text)) == false)
        {
            txtID_Account.Text = "";
            txtID_Account.Focus();
            MessageBox.Show("Account ID is the string of 12 number, Please check back Account ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            kt = false;
        }
        else if (Convert.ToString(txtID_Account.Text).Length.Equals(12) == false)
        {
            txtID_Account.Focus();
            MessageBox.Show("Account ID is the string of 12 number, Please check back Account ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            kt = false;
        }
        else if (tontai() == true)
        {
            txtID_Account.Text = "";
            MessageBox.Show("Account ID null, Please check back Account ID ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            kt = false;
        }
        return kt;
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * From tblAdmin Where (fldID_Admin = @fldID_Admin) And (fldPassword_Admin = @fldPassword_Admin)";
            command.Parameters.Add("@fldID_Admin", SqlDbType.NVarChar, 50).Value = txtID_Admin.Text;
            command.Parameters.Add("@fldPassword_Admin", SqlDbType.NVarChar, 50).Value = txtPassword_Admin.Text;
            da.SelectCommand = command;
            da.Fill(dtAdmin);
            
            if (dtAdmin.Rows.Count > 0)
            {
                frmAdministrator _frmAdministrator = new frmAdministrator();
                _frmAdministrator.Show();
                Hide();
            }
            else
            {
                if (MessageBox.Show("Login failed", "Login", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    txtID_Admin.Focus();
                }
                else
                {
                    this.tabInterfaceWelcome.SelectedIndex = 0;
                }
            }
        }

        private void frmInterfaceWelcome_Load(object sender, EventArgs e)
        {
            connect();
            txtID_Admin.Text = "Admin";
        }

        private void tabCustomer_Click(object sender, EventArgs e)
        {
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            kiemtraAccount();
            if (kt == true)
            {
                labID_Account.Visible = false;
                txtID_Account.Visible = false;
                btnNext.Visible = false;
                labPIN.Visible = true;
                txtPIN.Visible = true;
                btnLogin_Account.Visible = true;
                btnCancel.Visible = true;
            }
        }

        private void btnLogin_Account_Click(object sender, EventArgs e)
        {
            kiemtraPIN();
            if (kt == true)
            {
                //frmTransaction_Customer _frmTransaction_Customer = new frmTransaction_Customer();
                //_frmTransaction_Customer.Show();
                frmCustomer_Account _frmCustomer_Account = new frmCustomer_Account();
                _frmCustomer_Account.Show();
                Hide();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            labID_Account.Visible = true;
            txtID_Account.Visible = true;
            btnNext.Visible = true;
            labPIN.Visible = false;
            txtPIN.Visible = false;
            btnLogin_Account.Visible = false;
            btnCancel.Visible = false;
        }
    }
}
